package com.example.service;

import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.utils.RSAUtils;
import com.example.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SysPayServiceImpl implements SysPayService {

    Logger logger = LoggerFactory.getLogger(getClass());

    //apply url
    public static final String URL = "https://sitc.ink/pay/apply";

    //pri key
    public static final String prikey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALnQIrIQfIiy+6xtd5v+9XRUjSZlV5jfTXaLmYNy13vvff+f4SJyZnhOYOBo89AHLM8Ec/HzyPcLHLVMpatZZIkBiEWeeqX75MSKkvcqtr45I5NthzKFTpBjhT589t9WQmBET128sSMTq6uJNPf+K78RhRH2zbzHEujYP94CKSmBAgMBAAECgYBEQceFBtZgdvNJr3q5NMrg/XBOcvrfPuRbUEPHzEeJEPyFiysuNr0ao5wlw5gQypn0wCuBbW7dBVl55hqTm8Z4haLyk1pC30lHv8yD2c27wb5TgzFtpvXmgOqNvrE42CAHiKqBFKMaISaPgyOBFa/g4i4PKT2RPkfnnMvGOs8q8wJBAP24BZHG0RsWh+/PT3IMdmi/elIiMY5iEd+YIq+uHYLQSvnSN9aOr+xgF5CLQhTa4RgSH6Ng6Ir0GZjpyQJNs+MCQQC7e9ES1aIxO05IO9BcG+EWe1yesbuJ4AVz/bYaspU5hlgKq6mQO+NUoTr7QIZ/yw1pGQOc7uO1W8Hr2kTNWpJLAkA8uAF9NZttZCL8HRziKmFcCBb5+PzrGkU0wW/rozukxrxqtQhv7I3lf8mj6e/Sld8wp4Wyz8pXFrvZgHl+d6IJAkEAl9bGU9+rNP4X/05aCE8X89JDB89f2GETaYY/9s1NJ8KTCUlP0Z1/Fmphz1zJ0TPND6IPmAitaJMAshgbAslokwJAe3C6hyYUQ/Am+7i/ulMFv5JASD7uVyyoo6zNp1NDC6VbE6rWZLTYM9njFQyKg830pD4ZhrQIECZMPASqPZUY+w==\n";

    //pub key
    public static final String pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC50CKyEHyIsvusbXeb/vV0VI0mZVeY3012i5mDctd7733/n+EicmZ4TmDgaPPQByzPBHPx88j3Cxy1TKWrWWSJAYhFnnql++TEipL3Kra+OSOTbYcyhU6QY4U+fPbfVkJgRE9dvLEjE6uriTT3/iu/EYUR9s28xxLo2D/eAikpgQIDAQAB";

    //用户编号
    public static final String userId = "1";

    /****
     *
     * @param orderId 订单号
     * @param type 支付方式
     * @param attach 支付附言
     * @throws Exception
     */
    @Override
    public Result apply(String orderId, String type, String amount, String attach) throws Exception {
        RSA rsa = new RSA(prikey, pubkey);
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", RSAUtils.encrypt(rsa, orderId));
        params.put("type", RSAUtils.encrypt(rsa, type));
        params.put("amount", RSAUtils.encrypt(rsa, amount));
        params.put("attach", RSAUtils.encrypt(rsa, attach));
        params.put("userId", userId);
        Result result = JSONObject.parseObject(HttpUtil.post(URL, params), Result.class);
        result.setRsa(rsa);
        return result;
    }

    /****
     *
     * @param data
     * @param sign
     * @return
     */
    @Override
    public String callBack(String data, String sign) throws Exception {
        RSA rsa = new RSA(prikey, pubkey);
        logger.warn("数据解密: " + RSAUtils.decrypt(rsa, data));
        logger.warn("签名结果: " + RSAUtils.verify(RSAUtils.decrypt(rsa, data), rsa.getPublicKey(), sign));
        return RSAUtils.encrypt(rsa, "success");
    }
}
